<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include APPLICATION_PATH . "/models/Feedback.php";
include APPLICATION_PATH . "/models/User.php";
include APPLICATION_PATH."/models/News.php";
class KinhDoanh_FeedbackbkController extends Zend_Controller_Action{
      public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->modelMapper= new Model_FeedbackMapper();
        $this->model= new Model_Feedback();
        $this->UsermodelMapper= new Model_UserMapper();
        $this->Usermodel= new Model_User();
        $this->NewsmodelMapper= new Model_NewsMapper();
    }
       public function tAction(){}
public function traloitbkhAction(){
        $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
            $string=$this->_getParam("list_selectQuestion","");
            $t=  substr($string,0, strlen($string)-1);
            $this->view->list_id=$t;  
          
            //Đưa nội dung câu trả lời ra           
              $array_id =  explode(',',$t); 
               $count=0;
              foreach($array_id as $value){
                $id= $value;
                 if($count==2){
                       
                    $this->modelMapper->find($id,$this->model);
                    $this->view->getId=$this->model->getId();
                    $this->view->getTitle=$this->model->getTitle();
                    $this->view->getAnswer="Nội Dung Chung:".$this->model->getQuestion();
                     
                     break;
                                 
                 }
                $this->modelMapper->find($id,$this->model);
                $this->view->getId=$this->model->getId();
                $this->view->getTitle=$this->model->getTitle();
                $this->view->getAnswer=$this->model->getQuestion();
                $count++;
               
            }
                ////////////////////////////
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."kinhdoanh/feedback/index";            
            //tao mot mang id         
            if(isset($_POST["id"])){
               $array_id =  explode(',',$_POST["id"]);              
            } 
            ///
            foreach($array_id as $value){
                $id= $value;
                $this->modelMapper->find($id,$this->model);
                $getId=$this->model->getId();
                $getTitle=$this->model->getTitle();
                $getAnswer=$this->model->getQuestion();
                $getUserId=$this->model->getUser_Id();
                $getAdminId=$this->model->getAdmin_Id();
                if(empty($getId)){
                    $this->_redirect($redirectUrl);
                }
                /// 
                $this->model->setId($getId);
                $this->model->setTitle($getTitle);
                $this->model->setAnswer($getAnswer);
                $this->model->setUser_Id($getUserId);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By($getUserId);
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId());                 
//                   //neu la cau thong bao chung.tat ca cac user trong khu vuc deu co quyen tra loi
//                if($getUserId==0){
//                    //
//                    $getQuestion=$this->model->getQuestion();
////                    if($getQuestion==""){
////                        $getQuestion=$getQuestion;
////                    }else    
////                    {
//                        //cat ra
//                    $viTri_SOTU=strpos($getQuestion,"SOTU:");
//                    $vitri_NOIDUNG=strpos($getQuestion,"NOIDUNG:");
//                    $ID = substr($getQuestion,3,$viTri_SOTU-3);
//                    $SOTU = substr($getQuestion,$viTri_SOTU+5,$vitri_NOIDUNG-$viTri_SOTU-5);
//                    $NOIDUNG = substr($getQuestion,$vitri_NOIDUNG+8,strlen($getQuestion)-$vitri_NOIDUNG-8 );
//                        //them gia tri vao
//                        $user_login =GlobalLib::getUserLogin();
//                        $id_us=  $user_login["id"];
//                        $ID=$ID.$id_us.",";
//                        $SOTU=$SOTU.str_word_count(strip_tags($_POST["answer"])).",";
//                        $NOIDUNG=$NOIDUNG.strip_tags($_POST["answer"])."";
//                        //gan lai va luu vao csdl
//                    $getQuestion="ID:".$ID."SOTU:".$SOTU."NOIDUNG:".$NOIDUNG;
////                    }
//                        //
//                    if(strlen($_POST["answer"])>0){
//                       $this->model->setQuestion($getQuestion);
//                    } 
//                }else{
                     if(strlen($_POST["answer"])>0){
                        $this->model->setAnswer(strip_tags($_POST["answer"]));
                     }      
              //  }
                              
                   
                $this->model->setAdmin_Id($getAdminId);
                $this->modelMapper->save($this->model);
            }    
            
            $this->_redirect($redirectUrl);
            }     
           $this->view->item= $this->model; 
}

public function indexAction(){      
        //phan trang trong zend php 
            $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAll());
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            $string=$this->_getParam("list_selectQuestion","");
            $t=  substr($string,0, strlen($string)-1);          
            $this->view->list_id=$t;
            //Đưa nội dung câu trả lời ra           
             $array_id =  explode(',',$t); 
             $count=0;
             foreach($array_id as $value){
                $id= $value;
                 if($count==2){
                       
                    $this->modelMapper->find($id,$this->model);
                    $this->view->getId=$this->model->getId();
                    $this->view->getTitle=$this->model->getTitle();
                    $this->view->getAnswer=$this->model->getQuestion();
                     
                     break;
                                 
                 }
                $this->modelMapper->find($id,$this->model);
                $this->view->getId=$this->model->getId();
                $this->view->getTitle=$this->model->getTitle();
                $this->view->getAnswer=$this->model->getQuestion();
                $count++;
            }
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."kinhdoanh/feedback/listanswer";
          //tao mot mang id         
            if(isset($_POST["id"])){               
               $array_id =  explode(',',$_POST["id"]);              
            }          
            foreach($array_id as $value){
                $id= $value;
                $this->modelMapper->find($id,$this->model);
                $getId=$this->model->getId();
                $getTitle=$this->model->getTitle();
                $getQuestion=$this->model->getQuestion();
                $getUserId=$this->model->getUser_Id();
                if(empty($getId)){
                    $this->_redirect($redirectUrl);
                }
                $this->model->setId($getId);
                $this->model->setTitle($getTitle);
                $this->model->setQuestion($getQuestion);
                $this->model->setUser_Id($getUserId);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By($getUserId);
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId());          
                if(strlen($_POST["answer"])>0){
                    $this->model->setAnswer(strip_tags($_POST["answer"]));
                }          
                $this->model->setAdmin_Id(GlobalLib::getLoginId());
                $this->modelMapper->save($this->model);
            } 
            $this->_redirect($redirectUrl);
            }     
           $this->view->item= $this->model;
    }
    public function listanswerAction(){
           //phan trang trong zend php
           $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("title","title not like 'TB%'","modified DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            $string=$this->_getParam("list_selectQuestion","");
            $this->view->list_id=$string;
    }
     public function messgerAction(){ 
         $id_kd = GlobalLib::getLoginId();
         $this->view->my_user_list = $this->UsermodelMapper->fetchAllL($id_kd);
          $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
           $paginator = Zend_Paginator::factory($this->modelMapper->fetchAll());
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            $string=$this->_getParam("list_selectQuestion","");
            $this->view->list_id=$string;
            $myarray_id =  explode(',',$string);
            $list_user = "";
            foreach ($myarray_id as $my_id)
            {                
                if($my_id != "")
                {
                    $list_user = $list_user.GlobalLib::getName("user",$my_id, "username").";";                    
                }
            }            
            $this->view->list_user = $list_user;
//        ////////////////////////////
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."kinhdoanh/feedback/address";            
            //tao mot mang id
            //khong chọn thì gửi hết thông báo cho tất cả
            if(strlen($_POST["id"])<=0){
                 if(strlen($_POST["title"])>0){
                             $title='TBT'.$_POST["title"];
                             $this->model->setTitle($title);
                        }
                        if(strlen($_POST["question_kd"])>0){
                            $this->model->setAnswer(strip_tags($_POST["question_kd"]));
                        }               
                        $this->model->setUser_Id(0);
                        $this->model->setCreated(date("Y/m/d H:i:s"));
                        $this->model->setCreated_By(GlobalLib::getLoginId());
                        $this->model->setModified(date("Y/m/d H:i:s"));
                        $this->model->setModified_By(GlobalLib::getLoginId());          
                        $this->model->setQuestion("");
                        $this->model->setAdmin_Id(GlobalLib::getLoginId());
                        $this->modelMapper->save($this->model);
                
//                $t=$this->UsermodelMapper->fetchAllL($id_kd);
//                foreach($t as $value){
//                    $id= $value->id;                
//                    if($id!="")
//                    {
//                        if(strlen($_POST["title"])>0){
//                             $title='TB'.$_POST["title"];
//                             $this->model->setTitle($title);
//                        }
//                        if(strlen($_POST["question_kd"])>0){
//                            $this->model->setAnswer(strip_tags($_POST["question_kd"]));
//                        }               
//                        $this->model->setUser_Id($id);
//                        $this->model->setCreated(date("Y/m/d H:i:s"));
//                        $this->model->setCreated_By(GlobalLib::getLoginId());
//                        $this->model->setModified(date("Y/m/d H:i:s"));
//                        $this->model->setModified_By(GlobalLib::getLoginId());          
//                        $this->model->setQuestion("");
//
//                        $this->model->setAdmin_Id(GlobalLib::getLoginId());
//                        $this->modelMapper->save($this->model);
//                    }
//                }  
            }  else {    
                if(strlen($_POST["id"])>0){
                   $array_id =  explode(',',$_POST["id"]);              
                } 
                foreach($array_id as $value){
                    $id= $value;                
                    if($id!="")
                    {

                        if(strlen($_POST["title"])>0){
                             $title='TBT'.$_POST["title"];
                             $this->model->setTitle($title);
                        }
                        if(strlen($_POST["question_kd"])>0){
                            $this->model->setAnswer(strip_tags($_POST["question_kd"]));
                        }               
                        $this->model->setUser_Id($id);
                        $this->model->setCreated(date("Y/m/d H:i:s"));
                        $this->model->setCreated_By(GlobalLib::getLoginId());
                        $this->model->setModified(date("Y/m/d H:i:s"));
                        $this->model->setModified_By(GlobalLib::getLoginId());          
                        $this->model->setQuestion("");

                        $this->model->setAdmin_Id(GlobalLib::getLoginId());
                        $this->modelMapper->save($this->model);
                    }
                } 
            }
            $this->_redirect($redirectUrl);
        }     
            $this->view->item= $this->model;
    }
    public function addressAction(){ 
            $id_dn = GlobalLib::getLoginId();
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("substring(title,3)","title  like 'TBT%' and admin_id='". $id_dn."'","id DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
//            if($this->getRequest()->isPost()){                
//                  if(strlen($_POST["list_question"])>0){
//                     $string_id=$_POST["list_selectQuestion"];
//                     echo $string_id;
//                     exit();
//              } 
//            }
    }   
     public function address1Action(){ 
            $id_dn = GlobalLib::getLoginId();
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("substring(title,3)","title  like 'TBD%' and admin_id='". $id_dn."'","id DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
//            if($this->getRequest()->isPost()){                
//                  if(strlen($_POST["list_question"])>0){
//                     $string_id=$_POST["list_selectQuestion"];
//                     echo $string_id;
//                     exit();
//              } 
//            }
    }  
     public function addressquestionAction(){ 
            $id_dn = 0;
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("title","title  not like 'TB%' and admin_id='". $id_dn."' and answer =''","created ASC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            if($this->getRequest()->isPost()){                
                  if(strlen($_POST["list_question"])>0){
                     $string_id=$_POST["list_selectQuestion"];
                     echo $string_id;
                     exit();
              } 
            }
    }     
}